package beanstao.util.store.presenter;

import java.util.HashMap;

import beanstao.util.Sber;
import beanstao.util.interfaces.IManager;
import beanstao.util.log.LogHelper;
import beanstao.util.module.AbstractManager;
import beanstao.util.reflect.Mirror;

import com.google.common.collect.Maps;

@SuppressWarnings("unchecked")
public abstract class AbstractPresenterManager extends AbstractManager implements IManager
{
	/**
	 * 获取被管理的所有的Presenter的包名
	 * 
	 * @return
	 */
	protected abstract String getPresenterPackage();

	/**
	 * 各模块的PresenterManager的启动，继承自IManager的方法。<br>
	 */
	@Override
	public boolean initializes(final Object... objects)
	{
		return true;
	}

	@Override
	public boolean reStart()
	{
		boolean isStart = false;
		if (this.stop())
			isStart = this.start();
		return isStart;
	}

	@Override
	public boolean start()
	{
		return true;
	}

	@Override
	public boolean stop()
	{
		try
		{
			_PresenterMap.clear();
		}
		catch (Exception e)
		{
			this.logWarn("停止服务发生异常。", e);
			return false;
		}
		return true;
	}

	/**
	 * 获得Presenter的Class
	 * 
	 * @param clsName
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	protected Class getPresenterClass(final String clsName)
	{
		Sber sber = Sber.ME(this.getPresenterPackage());
		if (!".".equals(sber.end()))
			sber.append(".");
		sber.append(clsName);
		Class<?> klass = null;
		try
		{
			klass = Class.forName(sber.toString());
		}
		catch (ClassNotFoundException e1)
		{
			this.logWarn(sber.toString() + " 类未找到。", e1);
		}
		return klass;
	}

	// ========================

	protected static final HashMap<String, AbstractPresenter> _PresenterMap = Maps.newHashMap();
	protected static final HashMap<String, IPresenter> _IPresenterMap = Maps.newHashMap();
	protected static final Object initLock = new Object();

	/**
	 * 根据实例的名字创建Present实例
	 * 
	 * @param <PST>
	 * @param simpleName
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static <PST extends AbstractPresenter> PST createPresenter(final Class klass)
	{
		PST presenter = null;
		if (!_PresenterMap.containsKey(klass.getName()))
		{
			Mirror<PST> mirror = Mirror.me(klass);
			try
			{
				presenter = mirror.born();// 实例化
			}
			catch (Exception e)
			{
				LogHelper.logWarn(mirror, String.format("根据实例的名字创建Present实例,通过反射创建 { %s } 的实例发生异常。", klass));
			}
			synchronized (initLock)
			{
				_PresenterMap.put(klass.getName(), presenter);
				_IPresenterMap.put(klass.getName(), (IPresenter) presenter);
			}
			return presenter;
		}
		else
			return (PST) _PresenterMap.get(klass.getName());
	}

	public static <PST extends IPresenter> PST getPresenter(final String clsName)
	{
		if (!_IPresenterMap.containsKey(clsName))
		{
			try
			{
				createPresenter(Class.forName(clsName));
			}
			catch (ClassNotFoundException e)
			{
				LogHelper.logWarn(_IPresenterMap, "AbstractPresenterManager.getPresenter()异常。Class名：" + clsName, e);
			}

			return (PST) _IPresenterMap.get(clsName);
		}
		else
			return (PST) _IPresenterMap.get(clsName);
	}
}
